package com.ricky.game;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.ricky.config.ConfigData;
import com.ricky.config.scene.SceneConfig;
import com.ricky.core.net.abs.Request;
import com.ricky.game.scene.RoomController;
import com.ricky.game.scene.SceneController;

public class GameWorld implements Runnable {

	//*********************************** server ***********************
	/**
	 * 定时执行器
	 */
	public static ScheduledExecutorService timer=Executors.newScheduledThreadPool(2);
	//*********************************** private **********************
	
	/** 所有场景 */
	private Map<Integer,SceneController> scenes=new ConcurrentHashMap<Integer,SceneController>();
	
	/** 所有房间 */
	private Map<Integer,RoomController> rooms=new ConcurrentHashMap<Integer,RoomController>();

	/**
	 * 所有玩家的链接
	 */
	public static Map<String,Request> heroRequest=new ConcurrentHashMap<String,Request>();
	
	//************** public *****************************
	public GameWorld()
	{
		timer.schedule(this, 20, TimeUnit.MILLISECONDS);
	}
	
	/**
	 * 得到场景
	 * @param sceneId
	 * @return
	 */
	public SceneController getScene(int sceneId)
	{
		SceneController sc=scenes.get(sceneId);
		if(sc==null)
		{
			sc=new SceneController();
			SceneConfig  scf=ConfigData.getSceneConfig().get(sceneId+"");
			sc.init(scf);
			scenes.put(sceneId, sc);
		}
		return sc;
	}
	
	/**
	 * 得到英雄的连接
	 * @param HeroName
	 * @return
	 */
	public Request getHeroRequestByName(String HeroName)
	{
		return this.heroRequest.get("HeroName");
	}
	
	/**
	 * 得到所有房间
	 * @return
	 */
	public Map<Integer,RoomController> getRoom()
	{
		return rooms;
	}
	
	/**
	 * 得到房间
	 * @param roomId
	 * @return
	 */
	public RoomController getRoom(int roomId)
	{
		RoomController rc=rooms.get(roomId);
		return rc;
	}
	
	/**
	 * 添加房间到世界
	 */
	public void addRoomToWorld(RoomController rc)
	{
		rooms.put(rc.getId(), rc);
	}
	
	/**
	 * 从世界移除房间
	 */
	public void removeRoomFormWorld(RoomController rc)
	{
		rooms.remove(rc.getId());
	}
	
	/**
	 * 传送玩家到场景
	 * @param hero
	 * @param sceneId
	 */
	public void transport(Hero hero,int sceneId,int x,int y,int z){
		SceneController sc=getScene(sceneId);
		if(sc.prevEnter(hero,x,y,null))
		{
			int old=hero.getSceneID();
//			if(old!=sceneId)
//			{
				SceneController last=getScene(old);
				if(last!=null)
					last.remove(hero);
				sc.joinWaitingRoom(hero,x,y,z);
//			}
		}
	}
	
	/**
	 * 登录成功加入游戏世界
	 * @param hero
	 * @param req
	 */
	public void joinWorld(Hero hero,Request req)
	{
		hero.setRequest(req);
		heroRequest.put(hero.getName(), req);
	}
	
	/**
	 * 从游戏世界移除
	 * @param hero
	 */
	public void removeWorld(Hero hero)
	{
		heroRequest.remove(hero.getName());
	}
		
	@Override
	public void run() {
		Collection<SceneController> sc=scenes.values();
		for (SceneController item : sc)
			item.update();		
	}
	
	//********************* static ******************************
	private static GameWorld _instance;
	
	/**
	 * 游戏世界中的单例
	 * @return
	 */
	public static GameWorld getInstance()
	{
		if(_instance==null)
			_instance=new GameWorld();
		
		return _instance;
	}

}
